package com.cqit.bluetoothkeybox.service.impl;

import com.cqit.bluetoothkeybox.dao.UserDao;
import com.cqit.bluetoothkeybox.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl {

    @Autowired
    private UserDao userRepository;

    public List<User> getAllByOther(Example example) {
        List<User> users = userRepository.findAll(example);
        return users;
    }

    public Page<User> getPage(Integer pageNum, Integer pageLimit) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageLimit);
        return userRepository.findAll(pageable);
    }

    public Page<User> getPageSort(Integer pageNum, Integer pageLimit) {
        Sort sort = Sort.by(Sort.Direction.DESC, "old");
        Pageable pageable = PageRequest.of(pageNum - 1, pageLimit, sort);
        return userRepository.findAll(pageable);
    }

    public void savaUser(User user) {
        userRepository.save(user);
    }
}